clear

use "simdata_estimation.dta"
sort id

merge id using "simdata_base.dta"
tab _merge
drop _merge
sort id

merge id using "simdata_1term.dta"
tab _merge
drop _merge
sort id

merge id using "simdata_nss.dta"
tab _merge
drop _merge
sort id

merge id using "simdata_nobs_nun.dta"
tab _merge
drop _merge
sort id

merge id using "simdata_terms.dta"
tab _merge
drop _merge
sort id

merge id using "simdata_norun.dta"
tab _merge
drop _merge
sort id

save simdata_all.dta, replace

* CHANGE THE PARAMETERS ETA AND THETA USING RECENTLY ESTIMATED ONES
gen eta = 0.93753645875196
gen deltaa = 2
gen theta = 0.06921015804913

gen isim_imun_b = imun_b*1000+isim_b
tsset isim_imun_b tt_b

quietly foreach y in _e _b _1term _nss _nobs_nun _t _nr {
  gen new_fr_stolen`y' = newsteal`y'/funds`y'
  gen newqcons_pc`y' = newqcons`y'/(popsize`y'^eta)
  gen l1_educ`y' = l.ieduc`y'
}

gen actual_incumb_age_b = (incumb_age_b+7)*4   
gen college_b = l1_educ_b==2
replace college_b = . if l1_educ_b==.

save simdata_all.dta, replace

use simdata_all.dta, replace

log using selection.log, replace

****************
*** TABLE 7A ***
****************

* The numbers are in the output file of the SMM estimation

****************
*** TABLE 7B ***
****************

gen d_fr_stolen_audit_e = (fr_stolen_e > 0 & d_audit_e == 1)
gen d_fr_stolen_e = (newsteal_e > 0)

*** MATCHING MOMENTS ***
* Fraction Incumbents Not Stealing (We need to add audit3_e == 1 if we want to include tt_e == 3)
tab d_fr_stolen_e if (d_audit_e == 1 | audit3_e == 1) & tt_e >= 2
* Mean of Log Ability, Second Term
* SEE OUTPUT FOR ESTIMATED MODEL 
* The number is in the output file of the SMM estimation
* 25th, Median, 75th, 90th percentiles Fraction of Funds Solen
pctile pct_new_fr_stolen_e = new_fr_stolen_e if tt_e >= 2, nq(100)
list pct_new_fr_stolen_e in 1/100
* Per-capita public consumption using the rivalry parameter
sum newqcons_pc_e if tt_e>=2

*************************************
*** MARGINAL RATE OF SUBSTITUTION ***
*************************************

sum popsize_e, d
scalar m_popsize = r(mean)
sum newpcons_e
scalar m_newpcons = r(mean)
gen Emrs = - m_popsize^eta/(theta*m_newpcons^delta)
sum Emrs

***********************************************
*** INTERPRETATION OF MEAS ERR IN FINE DATA ***
***********************************************

* Get the following numbers from 
scalar mu_fine = 0.0942693
scalar delta_fine = -0.78912147063386
scalar actual_fine = mu_fine+delta_fine
scalar frac_fine = exp(actual_fine)
di frac_fine

********************************************************************************************
*** UTILITY OF BEING IN POWER AS FRACTION OF UTILITY FROM PRIVATE AND PUBLIC CONSUMPTION ***
********************************************************************************************

scalar u_power = 1.29937335365349
sum vemax_e
scalar m_vemax_e = r(mean)
di u_power/m_vemax_e

****************
*** TABLES 6 ***
****************

* TABLE 8: Fraction Stolen, Public Consumption, Incumbent's Age, Initial Wealth, College Education, Ability, Electoral Appeal
* NOTE THAT WE REPORT PER-CAPITA PUBLIC CONSUMPTION ADJUSTED FOR THE DEGREE OF RIVALRY
gen ieduc_b_new = ieduc_b-1
sum new_fr_stolen_b newqcons_pc_b actual_incumb_age_b psaving_b ieduc_b_new pit_b el_app_b if tt_b ==2
sum new_fr_stolen_b newqcons_pc_b actual_incumb_age_b psaving_b ieduc_b_new pit_b el_app_b if tt_b ==2 & nterms_b ==1
sum new_fr_stolen_b newqcons_pc_b actual_incumb_age_b psaving_b ieduc_b_new pit_b el_app_b if tt_b ==2 & nterms_b ==2

****************
*** TABLES 7 ***
****************

* TABLE 7: Decomposing Selection from Incentives and Decisions

*** FRACTION STOLEN ***

* Difference in the data
sum new_fr_stolen_b if tt_b ==2 & nterms_b ==1
scalar fr_stolen_1st_term = r(mean)

sum new_fr_stolen_b if tt_b ==2 & nterms_b ==2
scalar fr_stolen_2nd_term = r(mean)

scalar perc_tot = (fr_stolen_1st_term-fr_stolen_2nd_term)/fr_stolen_1st_term
di perc_tot

* Difference generated by electoral incentives (all observable and unobservable are identical)
sum new_fr_stolen_1term if tt_1term ==2 & nterms_1term ==1 & nterms_b == 1
scalar var_1term = r(mean)
scalar perc_ei = (fr_stolen_1st_term-var_1term)/fr_stolen_1st_term
di perc_ei

* Differences generated by initial savings
sum new_fr_stolen_nss if tt_nss ==2 & nterms_nss ==1 & nterms_b == 1
scalar var_nss = r(mean)
scalar perc_nss = (fr_stolen_1st_term-var_nss)/fr_stolen_1st_term
di perc_nss

* Difference generated by observables and unobservabless
sum new_fr_stolen_nobs_nun if tt_nobs_nun ==2 & nterms_nobs_nun ==1 & nterms_b == 1
scalar var_nobs_nun = r(mean)
scalar perc_nobs_nun = (fr_stolen_1st_term-var_nobs_nun)/fr_stolen_1st_term
di perc_nobs_nun

*** PUBLIC CONSUMPTION ***

* Difference in the data
sum newqcons_pc_b if tt_b ==2 & nterms_b ==1
scalar var_1st_term = r(mean)

sum newqcons_pc_b if tt_b ==2 & nterms_b ==2
scalar var_2nd_term = r(mean)

scalar perc_tot = (var_1st_term-var_2nd_term)/var_1st_term
di perc_tot

* Difference generated by electoral incentives (all observable and unobservable are identical)
sum newqcons_pc_1term if tt_1term ==2 & nterms_1term ==1 & nterms_b == 1
scalar var_1term = r(mean)
scalar perc_ei = (var_1st_term-var_1term)/var_1st_term
di perc_ei

* Differences generated by initial savings
sum newqcons_pc_nss if tt_nss ==2 & nterms_nss ==1 & nterms_b == 1
scalar var_nss = r(mean)
scalar perc_nss = (var_1st_term-var_nss)/var_1st_term
di perc_nss

* Difference generated by obervables and unobservables
sum newqcons_pc_nobs_nun if tt_nobs_nun ==2 & nterms_nobs_nun ==1 & nterms_b == 1
scalar var_nobs_nun = r(mean)
scalar perc_nobs_nun = (var_1st_term-var_nobs_nun)/var_1st_term
di perc_nobs_nun

**********************************************************************************************
*** NUMBERS FOR ABILITY IN FIRST, SECOND AND THIRD TERM FOR SUBSECTION 3-TERM=LIMIT POLICY ***
**********************************************************************************************

sum newit_t if tt_t>=2
sum newit_t if tt_t>=2 & nterms_t == 1
scalar m_ability_1st = r(mean)
sum newit_t if tt_t>=2 & nterms_t == 2
scalar m_ability_2nd = r(mean)
sum newit_t if tt_t>=2 & nterms_t == 3
scalar m_ability_3rd = r(mean)

di (m_ability_3rd - m_ability_1st)/m_ability_1st
di (m_ability_3rd - m_ability_2nd)/m_ability_2nd

****************************************************************************
*** NUMBERS TO EXPLAIN STEALING DECLINE FOR NO-RUN POLICY IN SECOND TERM ***
****************************************************************************

sum newit_nr if tt_nr>=2
sum newit_nr if tt_nr>=2 & nterms_nr == 1
scalar m_ability_1st = r(mean)
sum newit_nr if tt_nr>=2 & nterms_nr == 2
scalar m_ability_2nd = r(mean)

di (m_ability_2nd - m_ability_1st)/m_ability_1st

sum newsavings_nr if tt_nr>=2
sum newsavings_nr if tt_nr>=2 & nterms_nr == 1
scalar m_sav_1st = r(mean)
sum newsavings_nr if tt_nr>=2 & nterms_nr == 2
scalar m_sav_2nd = r(mean)

di (m_sav_2nd - m_sav_1st)/m_sav_1st

log close
